<?php
class Nc_model extends CI_Model {
    public function __construct() {
        $this->load->database();
    }
    
    public function set_nc($datos) {
        $this->db->insert('nc', $datos);
    }
    
    public function cantidad_nc_cerrada($cerrada) {
        $query = $this->db->query("SELECT COUNT(*) as cantidad
                            FROM
                                nc
                            WHERE
                                cerrada = $cerrada");
        return $query->row_array();
    }
    
    public function nc_por_estado($cerrada) {
        $query = $this->db->query("SELECT *
                            FROM
                                nc
                            WHERE
                                cerrada = $cerrada");
        return $query->result_array();
    }
    
    public function nc_derivadas($id) {
        $query = $this->db->query("SELECT *
                                    FROM
                                        nc
                                    WHERE
                                        derivar = $id");
        return $query->result_array();
    }
    
    public function update($datos, $id) {
        $idnc = array(
            'idnc' => $id
        );
        
        $this->db->update('nc', $datos, $idnc);
    }
    
    public function get_nc($idnc = FALSE) {
        if($idnc == FALSE) {
            $query = $this->db->query("SELECT * FROM nc");
            return $query->result_array();
        } else {
            $query = $this->db->query("SELECT *
                                        FROM
                                            nc
                                        WHERE
                                            idnc = $idnc");
            return $query->row_array();
        }
    }
    
    public function get_nc_abiertas() {
        $query = $this->db->query("SELECT * FROM nc WHERE cerrada = 0");
        return $query->result_array();
    }
    
    public function get_nc_cerradas() {
        $query = $this->db->query("SELECT * FROM nc WHERE cerrada = 1");
        return $query->result_array();
    }
    
    public function get_cantidad_abiertas_por_estado($estado) {
        $query = $this->db->query("SELECT COUNT(*) as cantidad
                            FROM
                                nc
                            WHERE
                                cerrada = 0 AND
                                estado = $estado");
        return $query->row_array();
    }
}
?>
